const currFullPath = function(){
	const pages = getCurrentPages();
	const route = pages[pages.length - 1];
	return route ? route.$page.fullPath : "";
};

const tabPages = [
    "/pages/index/index",
    "/pages/recommend/index",
    "/pages/cart/index",
    "/pages/mine/mine"
];

const redirect = function(redirect){
    let { url, mode = "redirectTo", param, success, fail, complete } = redirect;
    console.log('redirect === ', redirect)
    if (isUrl(url)) {
        // #ifdef H5
        window.open(url, "_blank");
        return;
        // #endif
        uni.navigateTo({
            url: `/pages/webviewPage/webviewPage?url=${url}`
        });
    }
    mode = mode;
	
    tabPages.includes(url) && (mode = "switchTab");
    mode != "switchTab" && param && Object.keys(param).length && (url += uni.$u.queryParams(param));
    switch (mode) {
        case "switchTab":
            uni.switchTab({
                url,
                success: () => {
                    success && success();
                },
                fail: () => {
                    fail && fail();
                },
                complete: () => {
                    complete && complete();
                }
            });
            break;
        case "navigateTo":
            uni.navigateTo({
                url,
                success: () => {
                    success && success();
                },
                fail: () => {
                    fail && fail();
                },
                complete: () => {
                    complete && complete();
                }
            });
            break;
        case "reLaunch":
            uni.reLaunch({
                url,
                success: () => {
                    success && success();
                },
                fail: () => {
                    fail && fail();
                },
                complete: () => {
                    complete && complete();
                }
            });
            break;
        case "redirectTo":
            uni.redirectTo({
                url,
                success: () => {
                    success && success();
                },
                fail: () => {
                    fail && fail();
                },
                complete: () => {
                    complete && complete();
                }
            });
            break;
    }
};

/**
 * 判断是否是url
 * @param str
 * @returns
 */
function isUrl(str){
    if (!str) return false;
    return str.indexOf("http://") != -1 || str.indexOf("https://") != -1;
}


export default {
	currFullPath,
	redirect,
	isUrl
}